#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int sum = tickets[k] * tickets.size();
        int Kval = tickets[k];
        auto it = tickets.begin();
        auto Kit = tickets.begin() + k;
        while (it != tickets.end())
        {
            if (*it < Kval)
            {
                sum -= Kval - *it;
            }
            else if (*it >= Kval && it > Kit)
            {
                --sum;
            }

            ++it;
        }

        return sum;
    }
};